package com.demo;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.swing.text.Document;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by etund on 2014/9/20.
 */
@WebServlet("/servlet/ServletDemo3")
public class Servlet_3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  response.setContentType("application/xml;charset=UTF-8");
                  PrintWriter out = response.getWriter();
                try{
                    //得到cities.xml的真是路径
                    String path = getServletContext().getRealPath("/cities.xml");

                    //得到Document对象
                    SAXReader reader = new SAXReader();
                    Document document =reader.read(path);
                    String xmlText = document.asXML();
                    out.write(xmlText);
                }catch (Exception e){
                    throw  new RuntimeException(e);
                }
    }
}
